
#If Not (PocketPC OrElse Smartphone) Then

Imports System.IO
Imports Microsoft.VisualBasic

Public Class binaryStream
    Inherits Stream

    Public Const closed As Int32 = 0
    Protected Friend filepoint As Int32
    Protected Friend offset As Int64

    Public Overrides ReadOnly Property CanRead() As Boolean
        Get
            Return Not EOF(filepoint) And filepoint <> closed
        End Get
    End Property

    Public Overrides ReadOnly Property CanSeek() As Boolean
        Get
            Return True
        End Get
    End Property

    Public Overrides ReadOnly Property CanWrite() As Boolean
        Get
            Return True
        End Get
    End Property

    Public Overrides Sub Flush()

    End Sub

    Public Overrides ReadOnly Property Length() As Long
        Get
            Return LOF(filepoint)
        End Get
    End Property

    Public Overrides Property Position() As Long
        Get
            Return offset
        End Get
        Set(ByVal value As Long)
            offset = value
        End Set
    End Property

    Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
        Dim i As Int32
        Dim rtn As Int32 = 0
        Dim bt As Byte
        If CanRead Then
            If buffer.Length() < count Then
                ReDim buffer(count - 1)
            End If
            For i = 0 To count - 1
                FileGet(filepoint, bt, offset + i)
                If EOF(filepoint) Then
                    Exit For
                End If
            Next
            rtn = i
        Else
            rtn = 0
        End If

        Return rtn
    End Function

    Public Overrides Function Seek(ByVal offset As Long, ByVal origin As System.IO.SeekOrigin) As Int64
        Select Case origin
            Case SeekOrigin.Begin
                Me.offset = offset
            Case SeekOrigin.Current
                Me.offset += offset
            Case SeekOrigin.End
                Me.offset = LOF(filepoint) - offset
        End Select

        Return Me.offset
    End Function

    Public Overrides Sub SetLength(ByVal value As Long)

    End Sub

    Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)

    End Sub

    Public Sub New()
        filepoint = closed
        offset = 0
    End Sub

    Public Sub New(ByRef filename As String, _
                    ByVal openAccess As OpenAccess, ByVal openShare As OpenShare)
        open(filename, openAccess, openShare)
    End Sub

    Public Sub open(ByRef filename As String, _
                            ByVal openAccess As OpenAccess, ByVal openShare As OpenShare)
        filepoint = FreeFile()
        FileOpen(filepoint, filename, OpenMode.Binary, openAccess, openShare)
        offset = 0
    End Sub

    Protected Overrides Sub Finalize()
        If filepoint <> closed Then
            FileClose(filepoint)
        End If

        MyBase.Finalize()
    End Sub
End Class

#End If
